import {observable, computed,action , /*toJS*/} from 'mobx';
import {Tools,Bubble} from 'weaver-mobile';
import { RouteLayout,Upload,Form } from 'weaver-mobile-page';
import * as API from '../../api/home/remember';
import {getEquipment,dataFormatting,regainMap} from '../../util/common';
import {toJS} from 'mobx'

const getLabel = Tools.getLabel;


export default class RememberStore {
    @observable loading = false;
    @observable hasRight = '';
    //当前设备
    equipment = {};

    @observable rememberGroup = [];
    @observable detailPageRoute = new RouteLayout.Store({id:'detailPage',route:'/rememberDetailPage'});
    @observable adRoute = new RouteLayout.Store({id:'advance',route:'/rememberDetailPage'});
    @observable toOcrRoute = new RouteLayout.Store({id:'toOcrRoute',route:'/rememberDetailPage'});
    @observable toAutoCheckRoute = new RouteLayout.Store({id:'toAutoCheckRoute',route:'/rememberDetailPage'});
    @observable toEnterByHand = new RouteLayout.Store({id:'toEnterByHand',route:'/rememberDetailPage'});

    //新建 编辑page
    @observable costTypeList = [];
    @observable form = {};
    @observable uploadStore = new Upload.Store();
    @observable typeParams = '';
    @observable loading_type=false;
    @observable imageId = 0;
    @observable icon = '';
    @observable title = '';
    @observable tjfp = '';
    @observable tjfj = '';
    @observable info = {};
    @observable tjfpfieldType = false; //false: towallt 单选  true: towallt 多选
    @observable hasDataNumber = 0;

    //添加发票
    @observable invoiceList = [];
    @observable invoiceIds = '';
    @observable uploadStore1 = new Upload.Store({ route: '/rememberDetailPage' });
    
    @observable fieldSelect = [];
    @observable fieldSelect = [];

    @observable invoiceResult = '';

    priceTaxSum = 0;   //价税合计

    //添加附件
    @observable datas = [];
    @observable valueList = [];
    @observable uploadParams = {};

    //发起报销
    @observable workflowSelect = [];

    @observable enabled = {}; 
    @observable tjfpInvoiceId = ''; //添加发票  中 选中的当前 ID 
    @observable cloudId = '';//添加发票  中 选中的当前 ID 


    initData = (value={}) =>{
        this.getTakeOneNoteType(value);
    }

    getEquipment = () =>{
        this.equipment = getEquipment();
    }
   

    //搜索 类型
    getTakeOneNoteType = (value) =>{
        this.loading = true;
        API.getTakeOneNoteType(value).then( data =>{
            const {status,costTypeList,errorInfo} = data;
            if(status === '1'){
                this.costTypeList = costTypeList;
            }else{
                Bubble.error(errorInfo)
            }
            this.rememberGroup = data.rememberGroup;
            this.loading = false;
        })
    }

    //记一笔对应类型  form 
    getTakeOneNoteSetPage = (value={},callBack) =>{
       this.loading_type = true;
        API.getTakeOneNoteSetPage(value).then( res =>{
            const {status,conditions} = res;
            if(status === '1'){
                this.form = new Form.Store({route:value.currentRoute});
                this.form.initForm(res.conditions,value.currentRoute);
                this.imageId = res.imageId;
                this.icon = res.icon;
                this.title = res.title;
                this.priceTaxSum = res.priceTaxSum;

                this.tjfp = res.tjfp;
                this.tjfj = res.tjfj;

                // if(value.valueId){
                //     const { formParams } = this.form;
                    
                //     this.hasDataNumber = res.hasDataNumber - (formParams[this.tjfp] ? 1 : 0) - (formParams[this.tjfj] ? 1 : 0);
                // }
                this.hasDataNumber = res.hasDataNumber

                this.tjfpfieldType = res.tjfpfieldType == '292' ? false : true; 
                
                conditions.map(c =>{
                    c.items.map(f =>{
                        if(f.domkey[0] == res.tjfj){
                            this.datas = f.datas ? f.datas : [];
                            this.valueList = f.valueList;
                            this.uploadParams = {
                                uploadUrl:f.uploadUrl,
                                maxFilesNumber:f.maxFilesNumber,
                                maxUploadSize:f.maxUploadSize,
                                multiSelection:f.multiSelection,
                            }
                           
                        }

                    })
                })

                callBack && callBack({valueList:this.valueList});
            }else{
                Bubble.error(res.errorInfo)
            }
            this.loading_type = false;
        }) 
    }

    //记一笔对应类型  添加附件 
    getTakeOneNoteSetInvoicePage = (value) =>{
        this.loading_type = true;
        API.getTakeOneNoteSetInvoicePage(value).then( data =>{
            const {status,errorInfo,mString} = data;
            if(status === '1'){
                this.invoiceList = data.invoiceList;
                this.invoiceIds = data.invoiceIds;
                if(mString){
                    Bubble.error(mString)
                }
            }else{
                Bubble.error(errorInfo)
            }
            
            this.loading_type = false;
        })
    }

    //附件转发票
    doCheckInvoicePage = (value,callBack) =>{
        this.loading_type = true;
        API.doCheckInvoicePage(value).then( data =>{
            const {status,uploadMsg} = data;
            if(status === '1'){
                if(uploadMsg){
                    Bubble.error(uploadMsg)
                }else{
                    Bubble.success(getLabel(517155,'成功'))
                    callBack && callBack({invoiceIds:data.invoiceIds});
                }
                
            }else{
                Bubble.error(uploadMsg)
            }
            
            this.loading_type = false;
        })
    } 

    //获取添加发票 附件 按钮   
    getTakeOneNoteSetselect = (value={}) =>{
        this.loading_type = true;
        API.getTakeOneNoteSetselect(value).then( data =>{
            const {status} = data;
            if(status === '1'){
                this.fieldSelect = data.fieldSelect;
                this.invoiceSelect = data.invoiceSelect;
            }else{
                Bubble.error(data.errorInfo)
            }
            
            this.loading_type = false;
        })
    }

    //获取编辑 时候 发起报销 按钮选项
    getInvoiceCreatWorkflow = (value={},callBack) =>{
        this.loading_type = true;
        API.getInvoiceCreatWorkflow(value).then( res =>{
            const {status,workflowSelect} = res;
            if(status == '1'){
                this.workflowSelect = workflowSelect;
            }else{
                Bubble.error(res.errorInfo)
            }

            this.loading_type = false;
        })
    }

    //发起流程
    creatWorkflow = (value,callBack) =>{
        this.loading_type = true;
        API.creatWorkflow(value).then( res =>{
            const {status,workflowSelect} = res;
            if(status == '1'){
                callBack && callBack({
                    requestId:res.requestId
                })
            }else{
                Bubble.error(res.errorInfo)
            }

            this.loading_type = false;
        })
    }     

    //保存
    saveTakeOneNoteSet = (value={},callBack) =>{
        this.loading_type = true;
        API.saveTakeOneNoteSet(value).then( data =>{
            const {status} = data;
            if(status == '1'){
                
                let tempInfo = JSON.parse(sessionStorage.getItem('rememberDetailPageInfo'));
                let params = Object.assign(tempInfo,{
                    valueId:data.valueId,
                })

                sessionStorage.setItem('rememberDetailPageInfo',JSON.stringify(params));
                callBack && callBack({valueId:data.valueId});
            }else{
                Bubble.error(data.errorInfo)
            }
            
            this.loading_type = false;
        })
    }

    //生成记账 获取数据
    invoiceCreatTakeOneNoteSet = (value={},callBack) =>{
        API.invoiceCreatTakeOneNoteSet(value).then( data =>{
            const {status} = data;
            if(status == '1'){
                callBack && callBack({valueId:data.valueId});
            }else{
                Bubble.error(data.errorInfo)
            }
        })
    }
    
    //删除当前发票
    delTakeOneNoteSet = (value,callBack) =>{
        this.loading_type = true;
        API.delTakeOneNoteSet(value).then( data =>{
            const {status} = data;
            if(status === '1'){
                callBack && callBack();
            }else{
                Bubble.error(data.errorInfo)
            }
            
            this.loading_type = false;
        }) 
    }

    //手动识别  自动查验
    getInvoiceInfoByCheck = (value={},callBack)=>{
        this.loading_type = true;
        API.getInvoiceInfoByCheck(value).then( res =>{
            const {status,invoiceResult} = res;
            if(status == '1'){
                if(invoiceResult){
                    this.invoiceResult = invoiceResult;
                    this.checkJson = res.checkJson;
                    callBack && callBack();
                }else{
                    Bubble.error(res.checkJson.errMsg);
                    
                }
            }else{
                Bubble.error(res.errorInfo);
            }
            this.loading_type = false;
        })
    }

    //删除报销提醒
    doDeleteRemind = (value,callBack) =>{
        this.loading_type = true;
        API.doDeleteReimbursementReminder(value).then( res=>{
            const {status} = res;
            if(status === '1'){
                Bubble.success(getLabel(20461, "删除成功"));
            }else{
                Bubble.error(res.errorInfo)
            }
            callBack && callBack();
            this.loading_type = false;
        })
    } 
    
    /*        发票云          */    
    getEnterInvoiceTap = (value) =>{
        API.getEnterInvoiceTap(value).then( res =>{
            const {status,invoiceCloud,data} = res;
            if(status == '1'){
                if(invoiceCloud){
                    const { Ocr,QRcode,EnterByHand,weChatImport } = invoiceCloud;
                    this.enabled = {
                        scanCode: QRcode,
                        photo:Ocr,
                        importFromWX: weChatImport,
                        validate:true
                    }
                }
            }
            
        })

    }
    
    setTypeParams = (value={}) =>{
        this.typeParams = value;
    }

    setInfo = (value={}) =>{
        this.info = value;
    }

    //改变上传数据源
    setDatas = (value)=>{
        this.datas = value;
    }

    //清除数据
    clearData = () =>{
        this.form = {};
        this.invoiceList = [];
        this.datas = [];
        this.info = {};
    }

    //添加发票  点击选中发票的ID 
    set_tjfpInvoiceId = (value) =>{
        this.tjfpInvoiceId = value;
    }

    //添加发票  点击选中发票的ID 
    set_cloudId = (value) =>{
        this.cloudId = value;
    }

    //改变invoiceIds 的值
    setInvoiceIds = (value = '')=>{
        this.invoiceIds = value;
    }
    setInvoiceList = (value=[]) =>{
        this.invoiceList = value;
    }
    /****************           记账本跳转过来               **************************/
    @observable detailPageRoute_AB = new RouteLayout.Store({id:'detailPage',route:'/accountBook/toRememberDetailPageRoute'});
    @observable adRoute_AB = new RouteLayout.Store({id:'advance',route:'/accountBook/toRememberDetailPageRoute'});
    @observable toOcrRoute_AB = new RouteLayout.Store({id:'toOcrRoute',route:'/accountBook/toRememberDetailPageRoute'});
    @observable toAutoCheckRoute_AB = new RouteLayout.Store({id:'toAutoCheckRoute',route:'/accountBook/toRememberDetailPageRoute'});
    @observable toEnterByHand_AB = new RouteLayout.Store({id:'toEnterByHand',route:'/accountBook/toRememberDetailPageRoute'});

    
}